package com.fw.ui.util.comparator;

import java.text.Collator;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;

import com.vaadin.data.util.DefaultItemSorter;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class TableItemSorter extends DefaultItemSorter {

   

    /**
     * 
     */
    private static final long serialVersionUID = 2499538661545847881L;

    public TableItemSorter(Comparator<Object> propertyValueComparator) {
	super(propertyValueComparator);
    }

    public static TableItemSorter getTableItemSorter() {
	return createInstance(null);
    }

    public static TableItemSorter getTableItemSorter(Locale locale) {
	return createInstance(locale);
    }

    private static TableItemSorter createInstance(Locale locale) {

	if (locale == null) {
	    locale = new Locale("en");
	}

	final Collator collatorInstance = Collator.getInstance(locale);

	TableItemSorter sorter = new TableItemSorter(new Comparator<Object>() {

	    @Override
	    public int compare(Object o1, Object o2) {
		if (o1 == null && o2 == null) {
		    return 0;
		} else if (o1 == null && o2 != null) {
		    return 1;
		} else if (o1 != null && o2 == null) {
		    return -1;
		} else {

		    if (o1 instanceof Button && o2 instanceof Button) {
			Button btn1 = (Button) o1;
			Button btn2 = (Button) o2;
			return collatorInstance.compare(btn1.getCaption(), btn2.getCaption());
		    } else if (o1 instanceof Link && o2 instanceof Link) {
			Link link1 = (Link) o1;
			Link link2 = (Link) o2;
			return collatorInstance.compare(link1.getCaption(), link2.getCaption());
		    } else if (o1 instanceof HorizontalLayout && o2 instanceof HorizontalLayout) {
			HorizontalLayout h1Layout = (HorizontalLayout) o1;
			HorizontalLayout h2Layout = (HorizontalLayout) o2;
			Component component1 = (Component) h1Layout.getComponent(0);
			Component component2 = (Component) h2Layout.getComponent(0);
			return this.compareComponent(component1, component2);
		    } else if (o1 instanceof VerticalLayout && o2 instanceof VerticalLayout) {
			VerticalLayout v1Layout = (VerticalLayout) o1;
			VerticalLayout v2Layout = (VerticalLayout) o2;
			Component component1 = (Component) v1Layout.getComponent(0);
			Component component2 = (Component) v2Layout.getComponent(0);
			return this.compareComponent(component1, component2);
		    } else if (o1 instanceof Label && o2 instanceof Label) {
			Label lbl1 = (Label) o1;
			Label lbl2 = (Label) o2;
			Object obj1 = lbl1.getData();
			Object obj2 = lbl2.getData();
			if (obj1 != null && obj2 != null) {
			    return this.compareObject(obj1, obj2);
			} else {
			    return collatorInstance.compare(lbl1.getCaption(), lbl2.getCaption());
			}
		    } else {
			return this.compareObject(o1, o2);
		    }
		}
	    }

	    private int compareObject(Object obj1, Object obj2) {
		if (obj1 == null && obj2 == null) {
		    return 0;
		} else if (obj1 == null && obj2 != null) {
		    return 1;
		} else if (obj1 != null && obj2 == null) {
		    return -1;
		} else {
		    if (obj1 instanceof String && obj2 instanceof String) {
			String str1 = (String) obj1;
			String str2 = (String) obj2;

			str1 = str1.replaceAll(" ", "");
			str2 = str2.replaceAll(" ", "");

//			boolean isNumber = false;
//			if (str1.indexOf(AbstractView.JPY_SYMBOLD) >= 0) {
//			    str1 = str1.replaceAll(AbstractView.JPY_SYMBOLD, "");
//			    isNumber = true;
//			} else if (str1.indexOf(AbstractView.USD_SYMBOLD) >= 0) {
//			    str1 = str1.replaceAll(AbstractView.USD_SYMBOLD, "");
//			    isNumber = true;
//			}
//
//			if (str2.indexOf(AbstractView.JPY_SYMBOLD) >= 0) {
//			    str2 = str2.replaceAll(AbstractView.JPY_SYMBOLD, "");
//			    isNumber = true;
//			} else if (str2.indexOf(AbstractView.USD_SYMBOLD) >= 0) {
//			    str2 = str2.replaceAll(AbstractView.USD_SYMBOLD, "");
//			    isNumber = true;
//			}
//
//			if (str1.indexOf(",") >= 0) {
//			    str1 = str1.replaceAll(",", "");
//			}
//			if (str2.indexOf(",") >= 0) {
//			    str2 = str2.replaceAll(",", "");
//			}
//
//			try {
//			    new Double(str1);
//			    new Double(str2);
//			    isNumber = true;
//			} catch (Exception e) {
//			    isNumber = false;
//			}
//
//			try {
//			    if (isNumber) {
//				if (str1.indexOf(".") >= 0 || str2.indexOf(".") >= 0) {
//				    return new Double(str1).compareTo(new Double(str2));
//				} else {
//				    return new Long(str1).compareTo(new Long(str2));
//				}
//			    } else {
//				return collatorInstance.compare(((String) obj1), ((String) obj2));
//			    }
//			} catch (Exception e) {
//			    return collatorInstance.compare(((String) obj1), ((String) obj2));
//			}

		    } else if (obj1 instanceof Long && obj2 instanceof Long) {
			return ((Long) obj1).compareTo(((Long) obj2));
		    } else if (obj1 instanceof Integer && obj2 instanceof Integer) {
			return ((Integer) obj1).compareTo(((Integer) obj2));
		    } else if (obj1 instanceof Double && obj2 instanceof Double) {
			return ((Double) obj1).compareTo(((Double) obj2));
		    } else if (obj1 instanceof Date && obj2 instanceof Date) {
			return ((Date) obj1).compareTo(((Date) obj2));
		    }
		}
		return 0;
	    }

	    private int compareComponent(Component c1, Component c2) {
		Object obj1 = null;
		Object obj2 = null;
		String caption1 = "";
		String caption2 = "";
		if (c1 instanceof Label && c2 instanceof Label) {
		    Label com1 = (Label) c1;
		    Label com2 = (Label) c2;
		    obj1 = com1.getData();
		    obj2 = com2.getData();
		    caption1 = com1.getCaption();
		    caption2 = com2.getCaption();
		} else if (c1 instanceof TextField && c2 instanceof TextField) {
		    TextField com1 = (TextField) c1;
		    TextField com2 = (TextField) c2;
		    obj1 = com1.getValue();
		    obj2 = com2.getValue();
		    caption1 = com1.getCaption();
		    caption2 = com2.getCaption();
		} else if (c1 instanceof Button && c2 instanceof Button) {
		    Button com1 = (Button) c1;
		    Button com2 = (Button) c2;
		    caption1 = com1.getCaption();
		    caption2 = com2.getCaption();
		} else if (c1 instanceof TextArea && c2 instanceof TextArea) {
		    TextArea com1 = (TextArea) c1;
		    TextArea com2 = (TextArea) c2;
		    obj1 = com1.getValue();
		    obj2 = com2.getValue();
		    caption1 = com1.getCaption();
		    caption2 = com2.getCaption();
		} else if (c1 instanceof ComboBox && c2 instanceof ComboBox) {
		    ComboBox com1 = (ComboBox) c1;
		    ComboBox com2 = (ComboBox) c2;
		    obj1 = com1.getValue();
		    obj2 = com2.getValue();
		    caption1 = com1.getCaption();
		    caption2 = com2.getCaption();
		}
		if (obj1 != null && obj2 != null) {
		    return this.compareObject(obj1, obj2);
		} else {
		    if (caption1 != null && caption2 != null) {
			return collatorInstance.compare(caption1, caption2);
		    }
		    return 0;
		}
	    }

	});

	return sorter;
    }
}
